package com.mida.app.report.controller;

import com.mida.app.report.service.DocFastWriteService;
import com.mida.app.report.service.MdmService;
import com.mida.bms.report.vo.PatientInfoParameterVo;
import com.mida.dms.knw.entity.vo.BodyTreeVo;
import com.mida.dms.knw.entity.vo.KnwNoumenonVo;
import com.mida.dms.knw.entity.vo.KnwSlotVo;
import com.mida.dms.knw.vo.BodyTreeParameterVo;
import com.mida.dms.knw.vo.KnwNoumenonPageVo;
import com.mida.dms.knw.vo.KnwSlotPageVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.tool.common.cons.RespConst;
import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.massert.MAssert;
import com.mida.tool.common.result.ListResult;
import com.mida.tool.common.ret.RespBody;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 快速写入
 */
@RestController
@RequestMapping("/docFastWrite")
@CrossOrigin
@Slf4j
public class DocFastWriteController {

    @Autowired
    private DocFastWriteService docFastWriteService;


    @Autowired
    private MdmService mdmService;

    /**
     * 根据当前登录用户的标识、获取模板列表
     * @param request
     * @param vo
     * @return
     */
    @ApiOperation(value = "根据当前登录用户的标识、获取模板列表",
            notes = "根据当前登录用户的标识、获取模板列表")
    @PostMapping("getDocStructTemplateList")
    @ResponseBody
    public ListResult getDocStructTemplateList(HttpServletRequest request,
                                               @RequestBody PatientInfoParameterVo vo) {
        try {
            // 获取当前登录用户的信息
            ListResult<UserDto> result = mdmService.getLoginUserInfo();
            UserDto userDto = result.getData();
            userDto.getEmployeeSoid();
            vo.setDoctorSoid(userDto.getEmployeeSoid());
            // 根据当前登录用户的标识、获取模板列表
            return docFastWriteService.getDocStructTemplateList(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("获取模板列表处理失败"));
        }
    }


    /**
     * 根据当前患者标识、指定报告发布状态、以及检查类型soid查询患者，历史报告记录列表
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "根据当前患者标识、指定报告发布状态、以及检查类型soid查询患者，历史报告记录列表",
            notes = "根据当前患者标识、指定报告发布状态、以及检查类型soid查询患者，历史报告记录列表")
    @PostMapping("getHistoryReportList")
    @ResponseBody
    public ListResult getHistoryReportList(@RequestBody PatientInfoParameterVo vo) {
        try {
            // 根据当前患者标识、指定报告发布状态、以及检查类型soid查询患者，历史报告记录列表
            return docFastWriteService.getHistoryReportList(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("获取历史报告记录列表处理失败"));
        }
    }

    /**
     * 根据当前登录用户信息标识、查询已授权的标准、精准片段
     *
     * @param request
     * @param vo
     * @return
     */
    @ApiOperation(value = "根据当前登录用户信息标识、查询已授权的标准、精准片段",
            notes = "根据当前登录用户信息标识、查询已授权的标准、精准片段")
    @PostMapping(value = "/getKnwNoumenonVoList", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<KnwNoumenonVo> getKnwNoumenonVoList(
            HttpServletRequest request, @RequestBody KnwNoumenonPageVo vo) {
        try {
            // 获取当前登录用户的信息
            ListResult<UserDto> result = mdmService.getLoginUserInfo();
            if (result != null && result.getData() != null) {
                UserDto userDto = result.getData();
                userDto.getEmployeeSoid();
                vo.setDoctorSoid(userDto.getEmployeeSoid());
            }
            // 根据当前登录用户信息标识、查询已授权的标准、精准片段
            return docFastWriteService.getKnwNoumenonVoList(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("查询标准、精准片段处理失败"));
        }
    }


    /**
     * 人体树根节点获取
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/getBodyRootNode", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<BodyTreeVo> getBodyRootNode(@RequestBody BodyTreeParameterVo vo) {
        try {
            // 人体树根节点获取
            return docFastWriteService.getBodyRootNode(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("人体树根节点获取处理失败"));
        }
    }

    /**
     * 人体部位子节点获取
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/getBodyNodes", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<BodyTreeVo> getBodyNodes(@RequestBody BodyTreeParameterVo vo) {
        try {
            // 人体部位子节点获取
            return docFastWriteService.getBodyNodes(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("人体部位子节点获取处理失败"));
        }
    }

    /**
     * 获取人体图列表
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/getBodyMaps", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult getBodyMaps(@RequestBody com.mida.dms.knw.vo.PatientInfoParameterVo vo) {
        try {
            return docFastWriteService.getBodyMaps(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("获取人体图列表处理失败"));
        }
    }

    /**
     * 取得推荐诊断列表
     *
     * @param vo
     * @return
     */
    @PostMapping("getRecommendDiagnosticList")
    @ResponseBody
    public ListResult getRecommendDiagnosticList(
            @RequestBody com.mida.dms.knw.vo.PatientInfoParameterVo vo) {
        try {
            return docFastWriteService.getRecommendDiagnosticList(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("取得推荐诊断列表处理失败"));
        }
    }


    /**
     * 根据部位soid、检查类型soid查询相关领域本体的soid
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/getNoumenonDomainSoids", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult getNoumenonDomainSoids(
            @RequestBody com.mida.dms.knw.vo.PatientInfoParameterVo vo) {
        try {
            return docFastWriteService.getNoumenonDomainSoids(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("根据部位soid、检查类型soid查询相关领域本体的soid处理失败"));
        }
    }

    /**
     * 1.查询所有本体
     */
    @ApiOperation(value = "查询所有本体", notes = "查询所有本体")
    @GetMapping(value = "/findAllNoumenon")
    @ResponseBody
    public ListResult<List<KnwNoumenonVo>> findAll(@RequestParam String search) {
        try {
            return docFastWriteService.findAll(search);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("查询所有本体处理失败"));
        }
    }

    /**
     * 2.分页查询本体
     */
    @ApiOperation(value = "分页查询本体", notes = "分页查询本体")
    @PostMapping(value = "/findPage", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<PageResultVo<KnwNoumenonVo>> findPage(@RequestBody KnwNoumenonPageVo vo) {
        try {
            return docFastWriteService.findPage(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("分页查询本体处理失败"));
        }
    }

    /**
     * 3.根据本体soid查询所有本体Slot
     */
    @ApiOperation(value = "查询所有本体Slot", notes = "查询所有本体Slot")
    @GetMapping(value = "/findAllSlot")
    @ResponseBody
    public ListResult<List<KnwSlotVo>> findAllSoltBynoumenonSoid(
            @RequestParam Long noumenonSoid) {
        ListResult listResult;
        try {
            // 根据本体soid查询所有本体Slot
            return docFastWriteService.findAllSoltBynoumenonSoid(noumenonSoid);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("查询所有本体Slot处理失败"));
        }
    }

    /**
     * 4.根据本体soid分页查询slot
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping(value = "/selectSlot", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<PageResultVo<KnwSlotVo>> findPageSlotBynoumenonSoid(
            @RequestBody KnwSlotPageVo vo) {
        try {
            // 根据本体soid分页查询slot
            return docFastWriteService.findPageSlotBynoumenonSoid(vo);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("根据本体soid分页查询slot处理失败"));
        }
    }

    /**
     * 5.根据本体soid获取本体
     *
     * @param soid
     * @return
     */
    @ApiOperation(value = "根据本体soid获取本体", notes = "根据本体soid获取本体")
    @ApiImplicitParam(name = "soid", value = "本体soid", required = false)
    @GetMapping(value = "/getKnwNoumenon")
    @ResponseBody
    public ListResult<KnwNoumenonVo> gainKnwNoumenon(@RequestParam Long soid) {
        try {
            // 根据本体soid获取本体
            return docFastWriteService.gainKnwNoumenon(soid);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("根据本体soid获取本体处理失败"));
        }
    }

    /**
     * 6.根据slotSoid查询slot
     *
     * @param soid
     * @return
     */
    @ApiOperation(value = "根据slotSoid查询slot", notes = "根据slotSoid查询slot")
    @ApiImplicitParam(name = "soid", value = "slotSoid", required = true)
    @GetMapping(value = "/getSlotBySlotSoid")
    @ResponseBody
    public ListResult<KnwSlotVo> findSlotBySlotSoid(@RequestParam Long soid) {
        try {
            // 根据slotSoid查询slot
            return docFastWriteService.findSlotBySlotSoid(soid);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("根据slotSoid查询slot处理失败"));
        }
    }
}

